package edu.ynu.se.xiecheng.achitectureclass.common.utils;

import edu.ynu.se.xiecheng.achitectureclass.common.dto.CommonDeviceValueDTO;
import okhttp3.*;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class CommonDeviceValueUtil {

    private static final String DEVICE_VALUE_URL = "https://fbox360.com/api/v2/dmon/value/get?boxNo=300023040198";
    private static final MediaType MEDIA_TYPE = MediaType.parse("application/json");

    public static List<CommonDeviceValueDTO> getDeviceValues(List<String> ids, String token) throws IOException {
        OkHttpClient client = new OkHttpClient();

        String json = new Gson().toJson(new DeviceValueRequest(ids));

        RequestBody body = RequestBody.create(json, MEDIA_TYPE);
        Request request = new Request.Builder()
                .url(DEVICE_VALUE_URL)
                .post(body)
                .header("Authorization", token)
                .header("Content-Type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

            String responseBody = response.body().string();
            Gson gson = new Gson();
            Type listType = new TypeToken<List<CommonDeviceValueDTO>>() {}.getType();
            return gson.fromJson(responseBody, listType);
        }
    }

    private static class DeviceValueRequest {
        private List<String> ids;

        public DeviceValueRequest(List<String> ids) {
            this.ids = ids;
        }

        public List<String> getIds() {
            return ids;
        }

        public void setIds(List<String> ids) {
            this.ids = ids;
        }
    }

    public static void main(String[] args) {
        try {
            List<String> ids = List.of("327061430532576244", "327061430532576247");
            String token = "Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg2QzQ2RTIxQTc0MTUxNTFCOTQ0MTY4MzhEMERGODU1OTZENkM2RTgiLCJ0eXAiOiJhdCtqd3QiLCJ4NXQiOiJoc1J1SWFkQlVWRzVSQmFEalEzNFZaYld4dWcifQ.eyJuYmYiOjE3MzYzNDIyODAsImV4cCI6MTczNjM0OTQ4MCwiaXNzIjoiaHR0cHM6Ly9hY2NvdW50LmZsZXhlbS5jb20vY29yZSIsImF1ZCI6Imlkc3ZyMyIsImNsaWVudF9pZCI6IjY4ZGIyZThiZGE4ZDQ3YjViNWRiN2VhZjcxYzdkY2RkIiwic3ViIjoiZTcxMjRhMTYtMjEwNS00ZTU4LWI5OGItOWE2ZmFkNjZiYjA3Iiwic2NvcGUiOlsiZmJveCJdfQ.cAqjog79sGQY7EUmhKmHLhTtNluc5I5wwRHD1-qwYsPLs_s_E3wptVEeG4tFJwTA9jbXVaEwQiOPcPVhFhrLtfGzX5ApEEFwkfoyA960EG4Mljtb0MvbT8n077PTAk9IeqWZClByhv37FJTRilSmk9ZHiXNDGye2eP0sE5qbkiSM4WNKFgg7sgPhzsRc9M-iUzsTIQfh25TKo0WhJ6wqqKDrEt-hsFbwQLIlClVRCkQMob1O02SUPWt83wI2D6E8YzWf-CqroD0owiZ1As2e38tRm2XSbJ0YghQDjf4rgVMMzMGXkvdxvSnu99AVnHRpBlcJ2gqI2ZWFskabolB7gQ";
            List<CommonDeviceValueDTO> commonDeviceValueDTOS = getDeviceValues(ids, token);
            for (CommonDeviceValueDTO commonDeviceValueDTO : commonDeviceValueDTOS) {
                System.out.println("ID: " + commonDeviceValueDTO.getId());
                System.out.println("Timestamp: " + commonDeviceValueDTO.getTimestamp());
                System.out.println("Data Type: " + commonDeviceValueDTO.getDataType());
                System.out.println("Name: " + commonDeviceValueDTO.getName());
                System.out.println("Value: " + commonDeviceValueDTO.getValue());
                System.out.println("Box ID: " + commonDeviceValueDTO.getBoxId());
                System.out.println("Status: " + commonDeviceValueDTO.getStatus());
                System.out.println("Connection State: " + commonDeviceValueDTO.getConnState());
                System.out.println("Connection State Timestamp: " + commonDeviceValueDTO.getConnStateTimestamp());
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
